/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dados;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Emanoel
 */
public class GerenciadorDados {

    private static ArrayList<DefinicoesDB> definicoesDB;

    public static ArrayList<DefinicoesDB> carregarDefinicoesDB() throws ParserConfigurationException, IOException {
        ArrayList<DefinicoesDB> retorno = new ArrayList<DefinicoesDB>();
        if (ValidadorXml.validar("dbconfig.xsd", "databases.xml")) {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse("databases.xml");
                NodeList databases = doc.getElementsByTagName("ns5:db");
                Element db = null;
                DefinicoesDB def = null;

                for (int i = 0; i < databases.getLength(); i++) {
                    db = (Element) databases.item(i);
                    NodeList l1 = db.getElementsByTagName("ns5:uniqueName");
                    NodeList l2 = db.getElementsByTagName("ns5:user");
                    NodeList l3 = db.getElementsByTagName("ns5:password");
                    NodeList l4 = db.getElementsByTagName("ns5:driver");
                    NodeList l5 = db.getElementsByTagName("ns5:url");

                    def = new DefinicoesDB();
                    def.setUniqueName(l1.item(0).getTextContent());
                    def.setUsername(l2.item(0).getTextContent());
                    def.setPassword(l3.item(0).getTextContent());
                    def.setDriver(l4.item(0).getTextContent());
                    def.setUrl(l5.item(0).getTextContent());
                    retorno.add(def);
                }
            } catch (Exception ex) {
                Logger.getLogger(GerenciadorDados.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        definicoesDB = retorno;
        return definicoesDB;
    }
}
